package com.wgz.flag.controller;

import com.wgz.flag.common.Result;
import com.wgz.flag.domain.dto.GroupChatDTO;
import com.wgz.flag.domain.vo.GroupChatVO;
import com.wgz.flag.service.GroupChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/group")
@CrossOrigin(origins = {"http://localhost:8081","http://localhost:8082"}, allowCredentials = "true")
@RequiredArgsConstructor
public class GroupChatController {

    private final GroupChatService groupChatService;

    /**
     * 创建群聊
     */
    @PostMapping("/create")
    public Result<GroupChatVO> createGroup(@RequestParam Long userId, @RequestBody GroupChatDTO groupChatDTO) {
        GroupChatVO groupChat = groupChatService.createGroup(userId, groupChatDTO);
        return Result.success(groupChat);
    }

    /**
     * 获取群聊列表
     */
    @GetMapping("/list")
    public Result<List<GroupChatVO>> getGroupList(@RequestParam Long userId) {
        List<GroupChatVO> groups = groupChatService.getGroupList(userId);
        return Result.success(groups);
    }

    /**
     * 更新群聊信息
     */
    @PutMapping("/update")
    public Result<String> updateGroup(@RequestParam Long groupId, @RequestBody GroupChatDTO groupChatDTO) {
        groupChatService.updateGroup(groupId, groupChatDTO);
        return Result.success("更新成功");
    }

    /**
     * 解散群聊
     */
    @DeleteMapping("/dismiss")
    public Result<String> dismissGroup(@RequestParam Long groupId, @RequestParam Long userId) {
        groupChatService.dismissGroup(groupId, userId);
        return Result.success("解散成功");
    }
}